package com.example.now_word;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.dao.StudyRecordDao;
import com.example.dao.SettingsDao;
import com.example.dao.WordRecordDao;
import com.example.dao.WordDao;
import com.example.dao.WordTypeDao;
import com.example.model.StudyRecord;
import com.example.utils.GetThemeStyle;
import com.example.utils.PieChartManagger;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieEntry;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

//首页
public class HomeFragment extends Fragment implements View.OnClickListener {

    private Button startstudyword;//开始学习按钮
    private TextView needreviewwordText;//今天还需要复习的单词数
    private TextView neednewwordText;//今天还需新学的单词书
    private TextView now_difficult_Text;//显示现在的难度
    private TextView now_all_word_value, now_have_finish_word_value;//总单词数和已背的单词数显示框

    private StudyRecordDao studyRecordDao;
    private WordDao wordDao;
    private WordRecordDao wordRecordDao;
    private SettingsDao settingsDao;
    private WordTypeDao wordTypeDao;

    private Banner banner;
    private List<Integer> image=new ArrayList<>();
    private List<String> title=new ArrayList<>();
    List<PieEntry> yvals = new ArrayList<>();
    List<Integer> colors = new ArrayList<>();

    private PieChart pieChart;


    private void initWisdomData() {
        image.clear();
        title.clear();
        image.add(Integer.valueOf(R.drawable.w0));
        image.add(Integer.valueOf(R.drawable.w1));
        image.add(Integer.valueOf(R.drawable.w2));
        image.add(Integer.valueOf(R.drawable.w3));
        image.add(Integer.valueOf(R.drawable.w4));
        image.add(Integer.valueOf(R.drawable.w5));
        image.add(Integer.valueOf(R.drawable.w6));
        image.add(Integer.valueOf(R.drawable.w7));
        image.add(Integer.valueOf(R.drawable.w8));
        image.add(Integer.valueOf(R.drawable.w9));
        title.add("w0");
        title.add("w1");
        title.add("w2");
        title.add("w3");
        title.add("w4");
        title.add("w5");
        title.add("w6");
        title.add("w7");
        title.add("w8");
        title.add("w9");
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setImageLoader(new MyImageLoader());
        banner.setImages(image);
        banner.setBannerAnimation(Transformer.Default);
        banner.isAutoPlay(true);
        banner.setBannerTitles(title);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.setDelayTime(3000);
        banner.setOnBannerListener(this::OnBannerClick);
        banner.start();
    }

    private void initPieData() {
        yvals.add(new PieEntry(wordDao.getTypeCount(), "总共单词"));
        yvals.add(new PieEntry(wordRecordDao.getTypeFinishWordCount(), "已背出单词"));
        double percent = 100 * (double) wordRecordDao.getTypeFinishWordCount() / (double) wordDao.getTypeCount();
        // 设置每份的颜色
        colors.add(Color.parseColor("#6785f2"));
        colors.add(Color.parseColor("#f5a658"));
        PieChartManagger pieChartManagger = new PieChartManagger(pieChart);
        pieChartManagger.showRingPieChart(yvals,colors,String.valueOf(percent));
    }

    public void OnBannerClick(int position) {
        Toast.makeText(getActivity(), "你点了第" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();
    }

    private class MyImageLoader extends ImageLoader {
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getContext().setTheme(GetThemeStyle.getThemeResId(getContext()));
        View view = inflater.inflate(R.layout.home_fragment, null);
        //获得Ui控件
        startstudyword = view.findViewById(R.id.startstudyword);
        neednewwordText = view.findViewById(R.id.new_text);
        needreviewwordText = view.findViewById(R.id.review_mastered);
        now_difficult_Text = view.findViewById(R.id.now_difficult);
        now_all_word_value = view.findViewById(R.id.now_all_word_value);
        now_have_finish_word_value = view.findViewById(R.id.now_have_finish_word_value);

        //初始化数据库操作类
        studyRecordDao = new StudyRecordDao(getContext());
        wordDao = new WordDao(getContext());
        wordRecordDao = new WordRecordDao(getContext());
        settingsDao = new SettingsDao(getContext());
        wordTypeDao=new WordTypeDao(getContext());

        banner = view.findViewById(R.id.banner);
        pieChart = view.findViewById(R.id.pie_chat1);
        initWisdomData();
        initPieData();

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    @Override
    public void onResume() {
        super.onResume();
        //从其他页面返回的时候，更新数据
        init();
    }

    private void init() {

        //更新页面上还需要背和复习的单词数量
        StudyRecord studyRecord = studyRecordDao.addOrGet();
        int needreviewwordCount = studyRecord.getNeedRepeatNum() - studyRecord.getRepeatNum();
        int neednewwordCount = studyRecord.getNeedNewNum() - studyRecord.getNewNum();
        if (neednewwordCount < 0) {
            neednewwordCount = 0;
        }
        neednewwordText.setText(String.valueOf(neednewwordCount));
        needreviewwordText.setText(String.valueOf(needreviewwordCount));

        //如果需复习和背的单词都为0，则修改按钮文本显示和按钮监听
        if(wordDao.getTypeCount()<=5){
            startstudyword.setText("本单词本单词数不够");
            startstudyword.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(wordTypeDao.find(settingsDao.getDifficulty())==null){
                        Toast.makeText(getContext(), "此单词本已被删除！", Toast.LENGTH_SHORT).show();
                        settingsDao.updateDifficulty("CET4");
                    }
                    Toast.makeText(getContext(), "本单词本单词数不够！", Toast.LENGTH_SHORT).show();
                }
            });
        }
        else if (neednewwordCount == 0 && needreviewwordCount == 0) {
            startstudyword.setText("你已完成今天的学习");
            startstudyword.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(wordTypeDao.find(settingsDao.getDifficulty())==null){
                        Toast.makeText(getContext(), "此单词本已被删除！", Toast.LENGTH_SHORT).show();
                        settingsDao.updateDifficulty("CET4");
                    }
                    Toast.makeText(getContext(), "你已经完成今日的学习，明天再来吧！", Toast.LENGTH_SHORT).show();
                }
            });
        } else {//设置页面上的按钮监听
            startstudyword.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(wordTypeDao.find(settingsDao.getDifficulty())==null){
                        Toast.makeText(getContext(), "此单词本已被删除！", Toast.LENGTH_SHORT).show();
                        settingsDao.updateDifficulty("CET4");
                    }
                    Intent intent = new Intent(getView().getContext(), MainStudyActivity.class);
                    startActivity(intent);
                }
            });
        }

        //设置页面上目前所选择的单词难度
        String now_difficult = settingsDao.getDifficulty();
        now_difficult_Text.setText(now_difficult);

        //设置总共单词数和已背单词数
        now_all_word_value.setText("" + wordDao.getTypeCount());
        now_have_finish_word_value.setText("" + wordRecordDao.getTypeFinishWordCount());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.now_difficult://点击目前难度选择难度

        }
    }
}